package one.four.three;

import java.util.*;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import edu.princeton.cs.algs4.Stopwatch;
import one.four.two.ThreeSum;

public class DoublingTest {
	private static final int MAXIMUM_INTEGER = 1000000;

	// This class should not be instantiated.
	private DoublingTest() {
	}

	public static double timeTrial(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = StdRandom.uniform(-MAXIMUM_INTEGER, MAXIMUM_INTEGER);
		}
		Stopwatch timer = new Stopwatch();
		ThreeSum.count(a);
		return timer.elapsedTime();
	}

	/**
	 * Prints table of running times to call {@code ThreeSum.count()} for arrays of
	 * size 250, 500, 1000, 2000, and so forth.
	 *
	 * @param args the command-line arguments
	 */
	public static void main(String[] args) throws Exception {

		List<Double> x = new ArrayList<Double>();
		List<Double> y = new ArrayList<Double>();
		StdDraw.setPenRadius(0.01);

		for (int n = 250; n <= 2000; n += n) {
			double time = timeTrial(n);
			StdOut.printf("%7d %7.1f\n", n, time);

			x.add(n + 0.0);
			y.add(time);
			if (x.size() >= 2) {

				StdDraw.setXscale(-1, n + 1.0);
				StdDraw.setYscale(-1, time + 1.0);
				for (int i = 0; i < x.size() - 1; i++) {
					StdDraw.line(x.get(i), y.get(i), x.get(i + 1), y.get(i + 1));
				}
				Thread.sleep(1000*1);
				StdDraw.clear();
			}
		}

	}
}